import java.awt.Dimension;
import java.awt.Font;
import java.awt.Rectangle;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * 
 *is the control class of search review Gui 
 */
public class SearchReviewC  implements ListSelectionListener
{
	/**array of strings for the review details */
	public String [] searchRe;
	/** array of strings for the returned result*/
	public String [] res;
	public DefaultListModel listModel;
	public String value;
	/**array of strings for details */
	public String [] review;
	/** array of strings for the returned result*/
	public String [] ureview;
	/** Indicate that a search was execute or not execute*/
	boolean sRflag=false;
	
	
	public DefaultListModel listModel1;
	public static JFrame details_frame;  //  @jve:decl-index=0:visual-constraint="10,34"
	/** new JPanel for the new details frame*/
	public JPanel dpanel;
	public JList jBookDetailsList;
	public JLabel Serial = null;
	public JLabel RTitle = null;
	public JScrollPane jScrollPane = null;
	public JLabel jLabelSerial = null;
	public JList jAuthorList = null;
	public JScrollPane jScrollAuthor = null;


	
	
	/**
	 * this method collect details to commit the book search and pass it forward.
	 * Also this method has a list listener on the result list,that find the author/s for the selected book.
	 * @param choice
	 */
	public void searchR(Integer choice) 
	{
		String temp = null;
		int i;
		searchRe = new String[6];
		searchRe[0]=Main.gusers.id;
		searchRe[1]="searchreview";
		searchRe[2]=Main.searchReviewG.getJTitleField().getText();
		searchRe[3]=Main.searchReviewG.getJAuthorField().getText();		
		searchRe[4]=Main.searchReviewG.getJKeyWordsField().getText();
		if (choice == 0)
			searchRe[5]="or";
		else
			searchRe[5]="and";
		if(searchRe[2].equals("") && searchRe[3].equals("") && searchRe[4].equals(""))
		{
   		 	JOptionPane.showMessageDialog(null,"All fields are empty, Please try again !","ERROR",JOptionPane.ERROR_MESSAGE);
   		 	return;
		}
		Main.log.connection.accept("searchR");
		while(Main.log.flag==false);
		Main.log.flag=false;
		temp=ChatClient.getRs();
		res=temp.split("\\*");
		if(temp.equals("error"))
    	{
    		 JOptionPane.showMessageDialog(null,"There is no such book in Catalog !","ERROR",JOptionPane.ERROR_MESSAGE);
    		 return;
    	}
		else
		{
			listModel=new DefaultListModel();
			for(i=0;i<this.res.length;i++)
				listModel.addElement(this.res[i]);
			Main.searchReviewG.jSearchRList= new JList(listModel);
			Main.searchReviewG.jSearchRList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
			Main.searchReviewG.jScrollreviewres.setViewportView(Main.searchReviewG.jSearchRList);
			Main.searchReviewG.jSearchRList.addListSelectionListener(this);
			Main.searchReviewG.jSearchRList.addMouseListener(new java.awt.event.MouseAdapter() 
			{
				public void mouseClicked(java.awt.event.MouseEvent e) 
				{
					String [] author;
					value = (String) Main.searchReviewG.jSearchRList.getSelectedValue();
					Main.welcomeScreen.d_Retrieve_Set(value);
					author = Main.welcomeScreen.res1;
					listModel=new DefaultListModel();
					for(int i=0;i<author.length;i++)
						listModel.addElement(author[i]);
					Main.searchReviewG.jAuthorList = new JList(listModel);
					Main.searchReviewG.jScrollResAuthor.setViewportView(Main.searchReviewG.jAuthorList);
					Main.searchReviewG.jAuthorList.setEnabled(false);
				}
			});
			Main.searchReviewG.updateUI();
			sRflag=true;
		}
	}


	@Override
	public void valueChanged(ListSelectionEvent arg0) 
	{
		// TODO Auto-generated method stub
		
	}
	/**
	 * this method get from the result list the selected book serial(chosen by the user),and get the book review,if exist.
	 * if review exists,for the selected book, this method will create anew frame and display the returned review/s.   
	 */

	public void getReview() 
	{
		String temp = null;
		String temp1 = null;
		String [] serial = null;
		String [] user = null;
		String [] tmpview = new String [100];
		String [] ureview=null;
		boolean flag=false;
		review = new String[3];
		temp1=(String) Main.searchReviewG.jSearchRList.getSelectedValue();
		serial=temp1.split("\\:");
		review[0]=Main.gusers.id;
		review[1]="getreview";
		review[2]=serial[0];
		Main.log.flag=false;
		Main.log.connection.accept("getreview");
		while(Main.log.flag==false);
		Main.log.flag=false;
		temp=ChatClient.getRs();
		if(temp.equals("error"))
    	{
    		 JOptionPane.showMessageDialog(null,"There is no reviews for this book !","ERROR",JOptionPane.ERROR_MESSAGE);
    		 return;
    	}
		else
		{
			res=temp.split("\\*");
			for (int i=0,j=0;i<res.length;i++,j+=2)
			{
				user=res[i].split("\\;");
				tmpview[j]=user[0];
				tmpview[j+1]=user[1];
			}
			int len;
			for (len=0;len<tmpview.length && !flag;len++)
			{
				if (tmpview[len] == null)
					flag=true;
			}
			ureview=new String[len-1];
			for (int j=0;j<ureview.length;j++)
				ureview[j]=tmpview[j];
			details_frame = new JFrame("Book Reviews");
			details_frame.setPreferredSize(new Dimension(700, 520));
			details_frame.setSize(new Dimension(680, 450));
			dpanel = new JPanel();
			dpanel.setLayout(null);
			Serial = new JLabel();
			Serial.setBounds(new Rectangle(200, 80, 60, 20));
			Serial.setText("Title :");
			RTitle = new JLabel();
			RTitle.setText(serial[1]);
			RTitle.setBounds(new Rectangle(240, 80, 150,20));
			
			JLabel welcomeLabel = new JLabel("Book Detals");
			welcomeLabel.setFont(new Font(Font.SANS_SERIF ,Font.ITALIC,20));
			welcomeLabel.setBounds(230,20,300,50);
			listModel1=new DefaultListModel();
			for (int i=0;i<ureview.length;i++)
				listModel1.addElement(ureview[i]);
			jBookDetailsList = new JList(listModel1);
			jBookDetailsList.setEnabled(false);
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(new Rectangle(70, 100, 520, 270));
			jScrollPane.setViewportView(jBookDetailsList);
		
	
			dpanel.add(welcomeLabel);
			dpanel.add(jScrollPane);
			dpanel.add(Serial, null);
			dpanel.add(RTitle, null);
			dpanel.setEnabled(true);
			dpanel.setVisible(true);
			details_frame.add(dpanel);
	       	details_frame.pack();
	       	details_frame.setVisible(true);
		}
	}
}

